package com.house365.core.thirdpart.auth.sina;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.text.TextUtils;

import com.house365.core.constant.CorePreferences;
import com.house365.core.json.JSONObject;
import com.house365.core.thirdpart.auth.AppKeyInfo;
import com.house365.core.thirdpart.auth.Thirdpart;
import com.house365.core.thirdpart.auth.WeiboApiRequest;
import com.house365.core.thirdpart.auth.WeiboAsyncRequest.RequestListener;
import com.house365.core.thirdpart.auth.WeiboException;
import com.house365.core.thirdpart.auth.WeiboHttpAPi;
import com.house365.core.thirdpart.auth.dto.AccessToken;

/**
 * @author py
 * @date 2012-11-6
 * @comment
 */
public class SinaWeiboApiRequest implements WeiboApiRequest {

	@Override
	public String post(WeiboHttpAPi httpApi, Thirdpart weibo,
			AppKeyInfo appkeyInfo, String content, String pic,
			RequestListener listener) throws WeiboException {
		String s;
		try {
			s = null;
			AccessToken accessToken = weibo.getAccessToken(Thirdpart.TYPE_SINA_WEIBO);
			if (!TextUtils.isEmpty(pic)) {
				// upload
				MultipartEntity entity = new MultipartEntity();
				entity.addPart(
						"access_token",
						new StringBody(accessToken.getToken(), Charset
								.forName(HTTP.UTF_8)));
				entity.addPart("status",
						new StringBody(content, Charset.forName(HTTP.UTF_8)));
				entity.addPart("pic", new FileBody(new File(pic)));
				s = httpApi.post(weibo.getUploadUrl(Thirdpart.TYPE_SINA_WEIBO), entity);
			} else {
				// update
				List<NameValuePair> oList = new ArrayList<NameValuePair>();
				oList.add(new BasicNameValuePair("access_token", accessToken
						.getToken()));
				oList.add(new BasicNameValuePair("status", content));
				s = httpApi.post(weibo.getUpdateUrl(Thirdpart.TYPE_SINA_WEIBO), oList);
			}
		} catch (Exception e) {
			CorePreferences.ERROR(e);
			throw new WeiboException("send weibo occurs exception", e);
		}
		int errCode = 0;
		String error = "";
		try {
			JSONObject json = new JSONObject(s);
			error = json.getString("error");
			errCode = json.getInt("error_code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (errCode != 0) {
			throw new WeiboException(getReadableMsg(error, errCode), errCode);
		}

		return s;
	}
	private static String getReadableMsg(String err, int errCode) {
		switch (errCode) {
		case 10009:
			err = "任务过多，系统繁忙！";
			break;
		case 10010:
			err = "任务超时！";
			break;
		case 10013:
			err = "不合法的微博用户！";
			break;
		case 10014:
			err = "应用的接口访问权限受限！";
			break;
		case 10018:
			err = "请求长度超过限制！";
			break;
		case 10022:
			err = "IP请求频次超过上限！";
			break;
		case 10023:
			err = "用户请求频次超过上限！";
			break;
		case 20006:
			err = "图片太大！";
			break;
		case 20008:
			err = "内容为空！";
			break;
		case 20012:
			err = "输入文字太长，请确认不超过140个字符！";
			break;
		case 20014:
			err = "安全检查参数有误，请再调用一次！";
			break;
		case 20015:
			err = "账号、IP或应用非法，暂时无法完成此操作！";
			break;
		case 20019:
			err = "不能重复分享相同内容！";
			break;
		case 20017:
			err = "不能重复分享相同内容！";
			break;
		case 21603:
			err =  "通知发送达到限制！";
			break;
		case 21301:
			err = "认证失败！";
			break;
		case 20032:
			err = "发布成功，目前服务器可能会有延迟，请耐心等待1-2分钟！";
			break;
		default:
			break;
		}
		
		return err;
	}
}
